package com.fametal.ui.utils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class IOUtils {
	
	private static final String TAG = "IOUtils";

	/* byte[]转Int */  
    public static int bytesToInt(byte[] bytes) {   
        int addr = bytes[0] & 0xFF;   
        addr |= ((bytes[1] << 8) & 0xFF00);   
        addr |= ((bytes[2] << 16) & 0xFF0000);   
        addr |= ((bytes[3] << 24) & 0xFF000000);   
        return addr;   
    }   
  
    /* Int转byte[] */  
    public static byte[] intToByte(int i) {   
        byte[] abyte0 = new byte[4];   
        abyte0[0] = (byte) (0xff & i);   
        abyte0[1] = (byte) ((0xff00 & i) >> 8);   
        abyte0[2] = (byte) ((0xff0000 & i) >> 16);   
        abyte0[3] = (byte) ((0xff000000 & i) >> 24);   
        return abyte0;   
    }   
    

 	private static String FILEPATH = "/sdcard";

 	public static File newFile(String fileName) {
 		File file = null;
 		try {
 			file = new File(FILEPATH, fileName);
 			file.delete();
 			file.createNewFile();
 		} catch (IOException e) {
 			// TODO Auto-generated catch block
 			e.printStackTrace();
 		}
 		return file;
 	}

 	public static void writeFile(File file, byte[] data, int offset, int count)
 			throws IOException {

 		FileOutputStream fos = new FileOutputStream(file, true);
 		fos.write(data, offset, count);
 		fos.flush();
 		fos.close();
 	}

 	public static byte[] readFile(String fileName) throws IOException {
 		File file = new File(FILEPATH, fileName);
 		file.createNewFile();
 		FileInputStream fis = new FileInputStream(file);
 		BufferedInputStream bis = new BufferedInputStream(fis);
 		int leng = bis.available();
 		Log.v(TAG, "filesize = " + leng);
 		byte[] b = new byte[leng];
 		bis.read(b, 0, leng);
 		// Input in = new Input(fis);
 		// byte[] ret = in.readAll();
 		// in.close();
 		bis.close();
 		return b;

 	}
}


